package zhiyou100.request;



import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.*;

@WebServlet("/request.action")
public class MyServletRequest extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取请求行的信息
        // 请求的方式
        String method = req.getMethod();
        System.out.println("请求方式为：" + method);// post
        // 请求的url地址:包含协议://ip地址(域名):端口号/虚拟目录/请求路径?参数列表(参数名称=参数值&参数名称2=参数值2&...)
        StringBuffer requestURL = req.getRequestURL();
        System.out.println("请求的URL地址信息为：" + requestURL);
        // 请求的URI信息: /虚拟路径/请求路径
        String requestURI = req.getRequestURI();
        System.out.println("请求的URI-请求路径为：" + requestURI);

        // 获取虚拟路径
        String contextPath = req.getContextPath();
        System.out.println("虚拟路径为：" + contextPath);// /day31

        // 虚拟路径
        //resp.sendRedirect(req.getContextPath()+"/请求路径");  可以动态的设置请求中的虚拟路径

        // 获取请求客户端对应的IP地址
        String remoteAddr = req.getRemoteAddr();
        System.out.println("客户端所在的主机对应的IP地址为：" + remoteAddr);
        String remoteHost = req.getRemoteHost();
        System.out.println("客户端对应的主机为：" + remoteHost);//remoteHost等于IP

        req.setCharacterEncoding("UTF-8");
        // 获取请求中的表单(form)数据  参数数据
        //http://192.168.217.212:8080/day31/request.action?username=%E5%BC%A0%E4%B8%89&password=123456
        //http://localhost:8080/day31/request.action?username=zhangsan&password=123456
        String param = req.getQueryString();
        String decode = URLDecoder.decode(param, "UTF-8");
        System.out.println("请求中的参数数据为：" + decode);//username=张三&password=123456

        // 获取请求头信息
        // 获取Cookie请求头数据
        String cookie = req.getHeader("Cookie");
        System.out.println("客户端请求头Cookie数据为：" + cookie);

        // 获取Host主机请求头数据
        String host = req.getHeader("Host");
        System.out.println("客户端请求头中主机的信息为：" + host);
        String agent = req.getHeader("User-Agent");
        System.out.println("客户端的内核和当前的操作系统信息为：" + agent);
        System.out.println("----------------------------------------");
        // 获取所有的请求头名称
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            System.out.println(headerName + "------>" + req.getHeader(headerName));
        }

        // 获取请求头中的referer信息
        String referer = req.getHeader("referer");
        System.out.println("客户端请求来源为："+referer);

        // 查看请求体内容，只能是post请求方式
        // 用户名和密码
        String username = req.getParameter("username");//用户名  表单中的name属性值
        String password = req.getParameter("password");// 密码
        System.out.println("用户名为：" + username);
        System.out.println("密码为：" + password);

        /*
            checkbox 复选框  name=hobby
         */

        // 获取所有的表单数据/url中对应的参数数据
        // ?username=%E5%BC%A0%E4%B8%89&password=123456
        Map<String, String[]> parameterMap = req.getParameterMap();
        // 遍历
        Set<String> names = parameterMap.keySet();
        // 迭代器
        Iterator<String> iterator = names.iterator();
        while (iterator.hasNext()) {
            String name = iterator.next();
            // 获取对应的value
            String[] values = parameterMap.get(name);
            // 查看数组
            System.out.println( name + "----->" +Arrays.toString(values));
        }



    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

}
